package com.zkx.db;

import java.sql.*;

public class DButil {
    private static final String url = "jdbc:mysql://localhost:3306/imooc?useUnicode=true&characterEncoding=utf8&useSSL=true";
    private static final String user = "root";
    private static final String password = "123456";
    private static Connection conn = null;
    private static Statement st = null;
    private static ResultSet rs = null;
    static{
        //1.加载驱动程序
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //2.获得数据库连接
        try {
            conn = DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        return conn;
    }
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //3.通过数据库的连接操作数据库，实现增删改查
        st = conn.createStatement();
        String sql = "select `user_name`,`age` from `imooc_goddess`";
        rs = st.executeQuery(sql);
        //rs.next() 返回true则rs中有数据
        while (rs.next()){
            System.out.println("user_name: "+rs.getObject("user_name"));
            System.out.println("age: "+rs.getObject("age"));
        }

    }
}
